<template>
  <div class="search-view">
    <div class="search-bar">
      <form>
        <van-icon name="search" class="search-icon" />
        <input
          class="input"
          v-model="value"
          @input="onSearch"
          type="text"
          autofocus="autofocus"
          placeholder="账号 / 手机号"
        />
        <button @click="onCancel">取消</button>
      </form>
    </div>

    <div class="search-warp" v-if="value">
      <div class="search-item" @click="goToBasicOrWant">
        <div class="img-box">
          <img src="@/assets/image/addressImg/icon15.png" alt="img" />
        </div>
        <div class="basic">
          搜索：<strong>{{ value }}</strong>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { Notify } from "vant";
import { mapActions, mapState } from "vuex";

export default {
  data() {
    return {
      value: "",
      friendsObj: {},
      obj: {},
    };
  },
  computed: {
    ...mapState({
      BasicObj: (state) => state.friends.friendsBasicObj,
    }),
  },
  watch: {
    BasicObj(newVal) {
      this.friendsObj = newVal;
    },
  },
  methods: {
    ...mapActions(["searchFriends"]),

    onSearch() {
      if (this.value.length == 11) {
        this.searchFriends(this.value);
      }
    },

    onCancel() {
      this.$router.go(-1);
    },
    goToBasicOrWant() {
      this.obj = this.friendsObj;
     
      if (JSON.stringify(this.obj) !== "{}") {
        
        if (this.obj.isFriends) {
          this.$router.push({
            name: "wantToChat",
            params: { list: JSON.stringify(this.obj) },
          });
        } else {
          this.$router.push({
            name: "basicMessage",
          });
        }
      } else {
        Notify(`${this.value}用户不存在`);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.search-view {
  background-color: rgb(17, 17, 17);
  width: 100%;
  height: 100vh;
  overflow: hidden;

  .search-bar {
    width: 100%;
    height: 55px;
    padding: 10px;

    form {
      width: 100%;
      height: 40px;
      display: flex;
      flex-direction: row;
      justify-content: space-around;
      align-items: center;
      position: relative;
      color: white;
      padding: 5px 0;

      .input {
        width: 75%;
        height: 100%;
        background-color: rgb(24, 24, 24);
        outline: none;
        border: none;
        text-indent: 30px;
      }

      .search-icon {
        position: absolute;
        left: 12px;
        top: 12px;
        font-size: 18px;
      }

      button {
        width: 60px;
        background-color: transparent;
        border: none;
        color: white;
      }
    }
  }

  .search-warp {
    color: white;
    width: 100%;
    height: 100%;
    margin-top: 10px;
    line-height: 20px;
    font-size: 13px;

    .search-item {
      display: flex;
      flex-direction: row;
      align-items: center;
      padding: 15px;
      background-color: rgb(24, 24, 24);
    }

    .img-box {
      width: 45px;
      height: 45px;
      background-color: gray;
      border-radius: 10px;
      margin-right: 15px;

      img {
        width: 100%;
        height: 100%;
        display: block;
         border-radius: 3px;
      }
    }
    .basic {
      strong {
        color: rgb(5, 193, 97);
        font-size: 14px;
      }
    }
  }
}
</style>